home *** CD-ROM | disk | FTP | other *** search
/ HyperLib 1997 Winter - Disc 1 / HYPERLIB-1997-Winter-CD1.ISO.7z / HYPERLIB-1997-Winter-CD1.ISO / オンラインウェア / BUS / TMCM Software and Labs.sit / Software for TMCM 7_95 / Files for Lab 9 / random walk < prev   
Text File  |  1994-05-08  |  2KB  |  41 lines

  1. { This program implements a "random walk" in which the
  2.   turtle repeatedly chooses a direction at random---either
  3.   up, down, left, or right---and moves one unit in that
  4.   direction.  It continues until it reaches the edge of
  5.   the 60-by-60 square that can be viewed in the program
  6.   xTurtle. }
  7.  
  8. DECLARE directionNum    { Gives a name to a memory location.  A value can
  9.                           be stored in this location using an assignment
  10.                           statement.  Later, that value can be accessed
  11.                           simply by refering to the name of the memory
  12.                           location.  A named memory location is called
  13.                           a "variable". }
  14.  
  15. LOOP
  16.  
  17.    directionNum := randomInt(4)   { Assignment statement stores a value into
  18.                                     the variable directionNum.  The value is
  19.                                     computed as the formula "randomInt(4)".  The
  20.                                     value of this formula is either 1, 2, 3
  21.                                     or 4, chosen at random; each possible choice
  22.                                     is equally likely. }
  23.  
  24.    IF directionNum = 1 THEN    { IF statement decides which direction to face }
  25.       face(0)
  26.    OR IF directionNum = 2 THEN        { Note that there are four possible }
  27.       face(90)                        { actions in this IF statement: face(0), }
  28.    OR IF directionNum = 3 THEN        { face(90), face(-90) and face(180).  Only }
  29.       face(-90)                       { one of these actions is taken, depending }
  30.    OR IF directionNum = 4 THEN        { on the value of the variable, directionNum. }
  31.       face(180)
  32.    END IF
  33.  
  34.    forward(1)    { and then the turtle moves forward one unit }
  35.  
  36.    EXIT IF (xcoord > 29) OR (xcoord < -29) OR (ycoord > 29) OR (ycoord < -29) 
  37.        { loop ends when the horizontal or vertical position of the turtle
  38.          moves out of the range -29 to 29 }
  39.  
  40. END LOOP
  41.